package com.yonyou.cms.epc.service.graphic.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yonyou.cms.epc.client.dto.graphic.GraphicInfoDTO;
import com.yonyou.cms.epc.client.entity.graphic.GraphicInfoPO;
import com.yonyou.cms.epc.mapper.graphic.GraphicInfoMapper;
import com.yonyou.cms.epc.service.graphic.GraphicInfoService;
import com.yonyou.cyx.framework.util.bean.ApplicationContextHelper;
import com.yonyou.cyx.function.exception.DALException;
import com.yonyou.cyx.function.utils.common.StringUtils;
import com.yonyou.cyxdms.framework.bean.dto.user_info.LoginInfoDto;
import com.yonyou.cyxdms.sysmanage.client.domains.dto.UserDictDto;
import com.yonyou.cyxdms.sysmanage.client.service.login.LoginService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xh
 * @since 2022-06-09
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class GraphicInfoServiceImpl extends ServiceImpl<GraphicInfoMapper, GraphicInfoPO> implements GraphicInfoService {
    //日志对象
    private static final Logger logger = LoggerFactory.getLogger(GraphicInfoServiceImpl.class);
    @Autowired
    GraphicInfoMapper graphicInfoMapper;
    @Autowired
    private LoginService loginService;

    /**
     * 分页查询对应数据
     * @param page 分页对象
     * @param queryCondition 查询条件
     * @return com.baomidou.mybatisplus.core.metadata.IPage<java.util.Map<java.lang.String,java.lang.Object>>
     * @author xh
     * @since 2018/7/22 0022
     */
    @Override
    @SuppressWarnings("unchecked")
    @Transactional(readOnly = true)
    public IPage<Map<String, Object>> selectPageBysql(Page page, Map<String, Object> queryCondition) {
        LoginInfoDto loginInfo = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        queryCondition.put("appId",loginInfo.getAppId());
        List<Map<String, Object>> graphicInfolList = graphicInfoMapper.selectPageBySql(page, queryCondition);
        for(Map<String,Object> map:graphicInfolList){
            UserDictDto createdUser = loginService.getUserById(Long.valueOf(map.get("createdBy").toString()));
            UserDictDto updatedUser = loginService.getUserById(Long.valueOf(map.get("updatedBy").toString()));
            if (updatedUser != null) {
                map.put("updatedByName", updatedUser.getUserName());
            }
            if (createdUser != null) {
                map.put("createdByName", createdUser.getUserName());
            }
        }
        page.setRecords(graphicInfolList);
        return page;
    }

    /**
     * 根据查询条件返回结果集
     * @param queryCondition 查询条件
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     * @author xh
     * @since 2018/7/22 0022
     */
    @Override
    @Transactional(readOnly = true)
    public List<Map<String,Object >> selectListBySql(Map<String, Object> queryCondition) {
        LoginInfoDto loginInfo = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        queryCondition.put("appId",loginInfo.getAppId());
        return graphicInfoMapper.selectListBySql(queryCondition);
    }

    /**
     * 根据查询条件返回结果集
     * @param id 主键ID
     * @return java.util.List<java.util.Map < java.lang.String , java.lang.Object>>
     * @author xh
     * @since 2018/7/22 0022
     */
    @Override
    @Transactional(readOnly = true)
    public Map<String, Object> getById(Integer id) {
        GraphicInfoPO graphicInfoPo = graphicInfoMapper.selectById(id);
        if(graphicInfoPo!=null){
            return graphicInfoPo.toMaps();
        }else{
            throw new DALException("查询失败,没有符合条件的数据");
        }
    }

    /**
     * 根据DTO 进行数据新增
     * @param graphicInfoDTO 页面DTO
     * @return void
     * @author zhangxianchao
     * @since 2018/7/21 0021
     */
    @Override
    public Map<String,Object> insert(GraphicInfoDTO graphicInfoDTO) {
        //对对象进行赋值操作
        GraphicInfoPO graphicInfoPo = graphicInfoDTO.transDtoToPo(GraphicInfoPO.class);
        //执行插入
        graphicInfoMapper.insert(graphicInfoPo);
        //返回插入的值
        return graphicInfoMapper.selectById(graphicInfoPo.getGraphicId()).toMaps();
    }

    /**
     * 根据DTO 及ID 进行数据更新
     * @param id 主键ID
     * @param graphicInfoDTO 页面DTO
     * @return void
     * @author zhangxianchao
     * @since 2018/7/21 0021
     */
    @Override
    public Map<String,Object> update(Integer id, GraphicInfoDTO graphicInfoDTO) {
        GraphicInfoPO graphicInfoPo = graphicInfoMapper.selectById(id);
        //对对象进行赋值操作
        graphicInfoDTO.transDtoToPo(graphicInfoPo);
        //执行更新
        graphicInfoMapper.updateById(graphicInfoPo);
        return graphicInfoPo.toMaps();
    }

    /**
     * 根据ID 进行删除数据
     * @param id
     * @return void
     * @author zhangxianchao
     * @since 2018/8/25 0025
     */
    @Override
    public void deleteById(Integer id) {
        boolean deleteCount = graphicInfoMapper.deleteBatch(id);
        if (!deleteCount) {
            throw new DALException("删除失败,没有符合条件的数据");
        }
    }

    /**
     * 根据ids 进行删除
     * @param ids
     * @return void
     * @author zhangxianchao
     * @since 2018/8/25 0025
     */
    @Override
    public void deleteBatchIds(String ids) {
        List<Integer> longList = StringUtils.convertStrToArray(ids,",", Integer.class);
        int deleteCount = graphicInfoMapper.deleteBatchIds(longList);
        logger.debug("删除数量：{}",deleteCount);
        if (deleteCount!=longList.size()) {
            throw new DALException("删除失败,没有符合条件的数据");
        }
    }
}
